﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using BLL.Common;
using Enum;
using System.Web.UI.HtmlControls;

namespace App
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pageinit();
        }

        protected void rptPolicyList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                PaperInfo p = e.Item.DataItem as PaperInfo;
                HtmlAnchor link = e.Item.FindControl("lb_Policy") as HtmlAnchor;

                if (p != null && p.PaperId > 0 && link != null)
                {
                    link.InnerText = p.Subject;
                    link.HRef = Contexts.AppPath + "/PolicyPager/PolicyPager.aspx?" + Contexts.PaperId + "=" + Contexts.GetEncrypt(p.PaperId.ToString());
                }

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptPolicyList.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                }
            }
        }

        protected void rptMemberRelation_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                PaperInfo p = e.Item.DataItem as PaperInfo;
                HtmlAnchor link = e.Item.FindControl("lb_MemberRelation") as HtmlAnchor;

                if (p != null && p.PaperId > 0 && link != null)
                {
                    link.InnerText = p.Subject;
                    link.HRef = Contexts.AppPath + "/MemberRelation/MemberRelationPaper.aspx?" + Contexts.PaperId + "=" + Contexts.GetEncrypt(p.PaperId.ToString());
                }

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptMemberRelation.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                }
            }
        }

        protected void rptRegionDynamic_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                PaperInfo p = e.Item.DataItem as PaperInfo;
                HtmlAnchor link = e.Item.FindControl("lb_RegionDynamic") as HtmlAnchor;

                if (p != null && p.PaperId > 0 && link != null)
                {
                    link.InnerText = p.Subject;
                    link.HRef = Contexts.AppPath + "/FamousRegion/FamousDynamicPaper.aspx?" + Contexts.PaperId + "=" + Contexts.GetEncrypt(p.PaperId.ToString());
                }

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptRegionDynamic.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                }
            }
        }

        protected void rptRegionNarrative_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                PaperInfo p = e.Item.DataItem as PaperInfo;
                HtmlAnchor link = e.Item.FindControl("lb_RegionNarrative") as HtmlAnchor;

                if (p != null && p.PaperId > 0 && link != null)
                {
                    link.InnerText = p.Subject;
                    link.HRef = Contexts.AppPath + "/FamousRegion/FamousNarrativePaper.aspx?" + Contexts.PaperId + "=" + Contexts.GetEncrypt(p.PaperId.ToString());
                }

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptRegionNarrative.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                }
            }
        }


        protected void rptNewThread_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                UserPaperInfo p = e.Item.DataItem as UserPaperInfo;
                HtmlAnchor link = e.Item.FindControl("lb_rptNewThread") as HtmlAnchor;
                //HtmlAnchor link = e.Item.FindControl("lb_rptMoreNewThread") as HtmlAnchor;
                if (p != null && p.UserPaperId > 0 && link != null)
                {
                    link.InnerText = p.Subject;
                    link.HRef = Contexts.AppPath + "/BBS/ThreadInfo.aspx?" + Contexts.ThreadId + "=" + Contexts.GetEncrypt(p.UserPaperId.ToString());
                }

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptNewThread.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                    ((HtmlAnchor)e.Item.FindControl("lb_rptMoreNewThread")).Visible = false;
                }
                else
                {
                    ((HtmlAnchor)e.Item.FindControl("lb_rptMoreNewThread")).Visible = true;
                    ((HtmlAnchor)e.Item.FindControl("lb_rptMoreNewThread")).HRef = Contexts.AppPath + "/BBS/ThreadList.aspx";
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = false;
                }
            }
        }

        protected void rptHotThread_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                UserPaperInfo p = e.Item.DataItem as UserPaperInfo;
                HtmlAnchor link = e.Item.FindControl("lb_rptHotThread") as HtmlAnchor;
                //HtmlAnchor link = e.Item.FindControl("lb_rptMoreNewThread") as HtmlAnchor;
                if (p != null && p.UserPaperId > 0 && link != null)
                {
                    link.InnerText = p.Subject;
                    link.HRef = Contexts.AppPath + "/BBS/ThreadInfo.aspx?" + Contexts.ThreadId + "=" + Contexts.GetEncrypt(p.UserPaperId.ToString());
                }

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptHotThread.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                    ((HtmlAnchor)e.Item.FindControl("lb_rptMoreHotThread")).Visible = false;
                }
                else
                {
                    ((HtmlAnchor)e.Item.FindControl("lb_rptMoreHotThread")).Visible = true;
                    ((HtmlAnchor)e.Item.FindControl("lb_rptMoreHotThread")).HRef = Contexts.AppPath + "/BBS/ThreadList.aspx";
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = false;
                }
            }
        }

        private void pageinit()
        {
            //绑定政策文件
            List<PaperInfo> Pages = BLL.BPaper.getInstance().GetAllPolicyPageTop10();
            if (Pages != null)
            {
                this.rptPolicyList.DataSource = Pages;
                this.rptPolicyList.DataBind();
            }
            //绑定会员动态
            Pages = BLL.BPaper.getInstance().GetAllUserRelationPageTop10();
            if (Pages != null)
            {
                this.rptMemberRelation.DataSource = Pages;
                this.rptMemberRelation.DataBind();
            }

            //
            List<UserPaperInfo> userPapers = BLL.BUserPaper.getInstance().GetPageNewBBS();
            if (userPapers != null)
            {
                this.rptNewThread.DataSource = userPapers;
                this.rptNewThread.DataBind();
            }
            userPapers = BLL.BUserPaper.getInstance().GetPageHotBBS();
            if (userPapers != null)
            {
                this.rptHotThread.DataSource = userPapers;
                this.rptHotThread.DataBind();
            }

            //获取全月专区信息
            List<FamousRegionInfo> regions = BLL.BFamousRegion.getInstance().GetAllFamousRegions();
            if (regions != null && regions.Count > 0)
            {
                FamousRegionInfo regionInfo = regions[0];
                lblFamousRegion.InnerText = regionInfo.Title;
                this.lbl_fUserName.Text = regionInfo.Name;
                this.lbl_fUserAddress.Text = regionInfo.Address;

                
                //
                fUserImg.Src = BLL.Common.UploadResource.GetPath(BLL.Common.BizResourceType.UserResource, BLL.Common.BizResourceAccess.Display) + regionInfo.ImageSrc;
                List<RegionCategoryInfo> categorys = BLL.BRegionCategory.getInstance().GetAllRegionCategoryByFamousRegionId(regionInfo.FamousRegionId);
                foreach (RegionCategoryInfo o in categorys)
                {
                    if (o.CategoryType == (int)CategoryType.PersonalDynamic)
                    {
                        this.link_Dynamic.HRef = Contexts.AppPath + "/FamousRegion/FamousDynamicPaperList.aspx?" + Contexts.FamousRegionId + "=" + Contexts.GetEncrypt(regionInfo.FamousRegionId.ToString()) + "&" + Contexts.RegionCategoryId + "=" + Contexts.GetEncrypt(o.RegionCategoryId.ToString());

                        
                        Pages = BLL.BPaper.getInstance().GetAllRegionCategoryTop10(o.RegionCategoryId);
                        if (Pages != null)
                        {
                            this.rptRegionDynamic.DataSource = Pages;
                            this.rptRegionDynamic.DataBind();
                        }
                    }
                    else if (o.CategoryType == (int)CategoryType.PersonalNarrative)
                    {
                        this.link_Narrative.HRef = Contexts.AppPath + "/FamousRegion/FamousNarrativePaperList.aspx?" + Contexts.FamousRegionId + "=" + Contexts.GetEncrypt(regionInfo.FamousRegionId.ToString()) + "&" + Contexts.RegionCategoryId + "=" + Contexts.GetEncrypt(o.RegionCategoryId.ToString());
                        Pages = BLL.BPaper.getInstance().GetAllRegionCategoryTop10(o.RegionCategoryId);
                        if (Pages != null)
                        {
                            this.rptRegionNarrative.DataSource = Pages;
                            this.rptRegionNarrative.DataBind();
                        }
                    }
                    else if (o.CategoryType == (int)CategoryType.PersonalResume)
                    {
                        Pages = BLL.BPaper.getInstance().GetAllRegionCategoryTop10(o.RegionCategoryId);
                        if (Pages != null && Pages.Count>0)
                        {
                            PaperInfo p = Pages[0];
                            this.div_resume.InnerHtml = p.Body;
                            linkMore.HRef = Contexts.AppPath + "/FamousRegion/FamousPerson.aspx?"+Contexts.PaperId+"="+Contexts.GetEncrypt(p.PaperId.ToString());
                        }
                    }
                }

            }
        }
    }
}